package com.ttai.ui.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import com.ttai.R;
import com.ttai.dagger.componet.activity.DaggerIpErrorComponet;
import com.ttai.dagger.module.activity.IpErrorPresenterModule;
import com.ttai.presenter.activity.IpErrorPresenter;
import com.ttai.ui.base.BaseActivity;
import com.ttai.ui.other.IpErrorDialog;
import com.ttai.ui.other.MessageDialog;
import com.ttai.untils.Constant;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.ButterKnife;

public class IpError extends BaseActivity {
    @Bind(R.id.tv_ipmessage)
    TextView tvIpmessage;
    private String ip;
    private String appName;
    private TextView ipmsg;
    private String ipMessage;
    private String pass;

    @Inject
    IpErrorPresenter ipErrorPresenter;

    //    MessageDialog messageDialog;
    IpErrorDialog ipErrorDialog;
    private Object phonenumber;
    private String appId;
    private String flag;
    private String token;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_iperror);
        ButterKnife.bind(this);
        setComponet();

        Intent intent = getIntent();
        ip = intent.getStringExtra("ip");
        appName = intent.getStringExtra("appName");
        ipmsg = (TextView) findViewById(R.id.tv_ipmessage);
        ipMessage = "您本次的登录访问   " + appName + "   的IP地址  " + ip + " 与历史记录不符，确认登录请点击允许，若非您本人操作，请点击不允许 ";
        appId = intent.getStringExtra("appId");
        flag = intent.getStringExtra("flag");
        token = Constant.getToken();

        ipErrorDialog = new IpErrorDialog(this);
        ipErrorDialog.setTitle("IP异常提醒");
        ipErrorDialog.setAcountMessage(appName);
        ipErrorDialog.setAdressMessage(ip);

//        messageDialog = new MessageDialog(this);
//        messageDialog.setTitle("IP异常提醒");
//        messageDialog.setMessage(ipMessage);
        try {
            ipErrorDialog.setYesOnclickListener("允许", new IpErrorDialog.onYesOnclickListener() {
                @Override
                public void onYesClick() {
                    pass = "Y";
                    ipErrorPresenter.ipError(token, ip, pass, appId, flag);
                    finish();
                    ipErrorDialog.dismiss();
                }
            });

            ipErrorDialog.setNoOnclickListener("不允许", new IpErrorDialog.onNoOnclickListener() {
                @Override
                public void onNoClick() {
                    pass = "N";
                    ip = "";
                    phonenumber = null;
                    ipErrorPresenter.ipError(token, ip, pass, appId, flag);
                    finish();
                    ipErrorDialog.dismiss();
                }
            });
            ipErrorDialog.show();
        } catch (Exception e) {
            this.finish();
        }

//        new AlertDialog.Builder(this)
//                .setMessage(ipMessage)
//                .setCancelable(true)
//                .setIcon(R.drawable.jieshao)
//                .setTitle("身份验证介绍")
//                .setPositiveButton("允许",  this)
//                .setNegativeButton("不允许",  this)
//                .show();

    }

    @Override
    public void next(View view) {

    }

    @Override
    public void pre(View view) {

    }

    @Override
    public void setComponet() {
        DaggerIpErrorComponet.builder()
                .ipErrorPresenterModule(new IpErrorPresenterModule(this))
                .build()
                .in(this);
    }

//    @Override
//    public void onClick(DialogInterface dialogInterface, int which) {
//        String token = Constant.getToken();
//        if (which ==DialogInterface.BUTTON_POSITIVE){
//            pass ="Y";
//            ipErrorPresenter.ipError(token,ip,pass,appId,flag);
//            finish();
//        }
//        else if (which == DialogInterface.BUTTON_NEGATIVE){
//            pass ="N";
//            ip=null;
//            phonenumber =null;
//            ipErrorPresenter.ipError(token,ip,pass,appId,flag);
//            finish();
//        }
//    }
}
